﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Text;

namespace CustomActionLibrary
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            try
            {
                string fullPath = Environment.GetEnvironmentVariable("PROGRAMDATA") + "\\Autodesk\\Revit\\Addins\\2014\\RoomFinishes.addin";
                StreamReader reader = new StreamReader(fullPath);
                XDocument xmlDoc = XDocument.Load(new XmlTextReader(reader));
                reader.Close();
                XElement addins = xmlDoc.Document.Element("RevitAddIns");
                XElement addin = addins.Element("AddIn");
                XElement a = addin.Element("Assembly");
                a.Value = Environment.GetEnvironmentVariable("PROGRAMW6432") + "\\YarUnderoaker\\RoomFinishesAddIn2014\\RoomFinishesAddIn.dll";
                StreamWriter writer = new StreamWriter(fullPath);
                xmlDoc.Save(new XmlTextWriter(writer));
                writer.Close();
            }
            catch (Exception ex)
            {
                string logFile = Environment.GetEnvironmentVariable("HOMEDRIVE") + "\\RoomFinishesAddIn.log";
                File.WriteAllText(logFile, string.Format("{0} - {1}", DateTime.Now.Ticks.ToString(), ex.Message), Encoding.UTF8);
            }

        }
    }
}
